﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Business.Stores;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.EnumClassifiers;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class PointOfSalesPresenter
    {
        private IViews.IPointOfSales pViewToPresent;

        public PointOfSalesPresenter(IViews.IPointOfSales managedView)
        {
            if (managedView == null)
            {
                throw new Exception("managedView cannot be null");
            }

            pViewToPresent = managedView;
            pViewToPresent.OnLoadView += new Innova.FoundationFramework.DataTypes.GenericEventHandler<long>(vManagedView_OnLoadView);
            pViewToPresent.OnSearchProduct += new Innova.FoundationFramework.DataTypes.GenericEventHandler<string>(vManagedView_OnSearchProduct);
            pViewToPresent.OnSale += new EventHandler(pViewToPresent_OnSale);

        }

        void vManagedView_OnLoadView(object sender, Innova.FoundationFramework.DataTypes.GenericEventArgs<long> e)
        {

        }

        void vManagedView_OnSearchProduct(object sender, Innova.FoundationFramework.DataTypes.GenericEventArgs<string> e)
        {
            ProductManager pProductManager = new ProductManager();
            
            Product resultProduct = pProductManager.GetProductByModelEntireWord(e.EventData.Trim());
            if (resultProduct != null)
            {
                pViewToPresent.AddProductToSale = LoadSaleOrderDetail(resultProduct);
                return;
            }

            Product result = pViewToPresent.InvokeEditionView(e.EventData);
            if (result != null)
            {
                pViewToPresent.AddProductToSale = LoadSaleOrderDetail(result);
            }
        }

        void pViewToPresent_OnSale(object sender, EventArgs e)
        {
            if (pViewToPresent.InvokeSaleView(pViewToPresent.ColProductToSale))
            {
                pViewToPresent.ClearWindow();
            }
        }

        private SalesOrderDetail LoadSaleOrderDetail(Product result)
        {
            ProductPriceManager vProductPriceManager = new ProductPriceManager();
            ProductPrice objProductPrice = new ProductPrice();
            SalesOrderDetail newSalesOrderDetail = new SalesOrderDetail();

            objProductPrice = vProductPriceManager.GetProductPriceByProduct(result.IdProduct);
            Classifier InStock = new Classifier();
            InStock.Name = result.OtherDescription;

            newSalesOrderDetail.IdProduct = result.IdProduct;
            newSalesOrderDetail.Product = result;
            newSalesOrderDetail.IdcState = (long)SalesOrderDetailState.Sale;
            newSalesOrderDetail.OrderQuantity = 1;
            newSalesOrderDetail.UnitPrice = objProductPrice.SalePrice;
            newSalesOrderDetail.UnitPriceReal = 0;
            newSalesOrderDetail.WithGuarantee = result.WithGuarantee;
            newSalesOrderDetail.Classifier = InStock;

            return newSalesOrderDetail;
        }

    }
}
